﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace _表达式树测试.Helper
{
    public class WhereHelper<T> where T : class
    {
        public BinaryExpression filter;
        public ParameterExpression param;

        public WhereHelper()
        {
            param = Expression.Parameter(typeof(T), "c");
            Expression left = Expression.Constant(1);
            filter = Expression.Equal(left, left);

            //Expression Left = Expression.Property(param, typeof(T).GetProperty("IsDelete"));
            //Expression Reight = Expression.Constant(false, false.GetType());
            //Expression result = Expression.Equal(Left, Reight);
            //filter = Expression.And(Left, Reight);


        }

        public Expression<Func<T, bool>> GetExperssion()
        {
            return Expression.Lambda<Func<T, bool>>(filter, param);
        }

        /// <summary>
        /// 等于
        /// </summary>
        /// <param name="propertyName"></param>
        /// <param name="value"></param>
        public void EqualExperssion(string propertyName, object value)
        {
            Expression left = Expression.Property(param, typeof(T).GetProperty(propertyName));
            Expression reight = Expression.Constant(value, value.GetType());
            Expression result = Expression.Equal(left, reight);
            filter = Expression.And(filter, result);
        }

        /// <summary>
        /// 包含
        /// </summary>
        /// <param name="propertyName">反射的类型字符串名称</param>
        /// <param name="value">值</param>
        public void ContainsExperssion(string propertyName, object value)
        {
            Expression left = Expression.Property(param, typeof(T).GetProperty(propertyName));
            Expression reight = Expression.Constant(value, value.GetType());
            Expression result = Expression.Call(left, typeof(string).GetMethod("Contains"), reight);
            filter = Expression.And(filter, result);
        }

        /// <summary>
        /// 大于
        /// </summary>
        /// <param name="propertyName">反射的类型字符串名称</param>
        /// <param name="value">值</param>
        public void GreaterThanExperssion(string propertyName, object value)
        {
            Expression left = Expression.Property(param, typeof(T).GetProperty(propertyName));
            Expression reight = Expression.Constant(value, value.GetType());
            Expression result = Expression.GreaterThan(left, reight);
            filter = Expression.And(filter, result);
        }

        /// <summary>
        /// 小于
        /// </summary>
        /// <param name="propertyName"></param>
        /// <param name="value"></param>
        public void LessThanExperssion(string propertyName, object value)
        {
            Expression left = Expression.Property(param, typeof(T).GetProperty(propertyName));
            Expression reight = Expression.Constant(value, value.GetType());
            Expression result = Expression.LessThan(left, reight);
            filter = Expression.And(filter, result);
        }


        /// <summary>
        /// 大于等于
        /// </summary>
        /// <param name="propertyName">反射的类型字符串名称</param>
        /// <param name="value">值</param>
        public void GreaterThanOrEqualExperssion(string propertyName, object value)
        {
            Expression left = Expression.Property(param, typeof(T).GetProperty(propertyName));
            Expression reight = Expression.Constant(value, value.GetType());
            Expression result = Expression.GreaterThanOrEqual(left, reight);
            filter = Expression.And(filter, result);
        }

        /// <summary>
        /// 小于等于
        /// </summary>
        /// <param name="propertyName"></param>
        /// <param name="value"></param>
        public void LessThanOrEqualExperssion(string propertyName, object value)
        {
            Expression left = Expression.Property(param, typeof(T).GetProperty(propertyName));
            Expression reight = Expression.Constant(value, value.GetType());
            Expression result = Expression.LessThanOrEqual(left, reight);
            filter = Expression.And(filter, result);
        }


    }
}
